package me.android.library.utils.android;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@UtilityClass
public class SoftInputUtil {

    /**
     * 强制显示软键盘
     */
    public void show(View view) {
        // 接受软键盘输入的编辑文本或其它视图
        SystemServiceUtil.getInputMethodManager().showSoftInput(view, InputMethodManager.SHOW_FORCED);
    }

    /**
     * 强制隐藏软键盘
     */
    public void hide(View view) {
        // 接受软键盘输入的编辑文本或其它视图
        SystemServiceUtil.getInputMethodManager().hideSoftInputFromInputMethod(view.getApplicationWindowToken(), 0);
    }


    /**
     * 强制隐藏软键盘
     */
    public void hide(Activity atv) {
        final View view = atv.getCurrentFocus();
        if (view != null && view.getWindowToken() != null) {
            SystemServiceUtil.getInputMethodManager().hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
        }
    }

    /**
     * 如果输入法打开则关闭，如果没打开则打开
     */
    public void toggleInput(Context cx) {
        SystemServiceUtil.getInputMethodManager().toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 获取输入法打开的状态
     */
    public boolean isOpen(Context cx) {
        InputMethodManager imm = SystemServiceUtil.getInputMethodManager();
        return imm.isActive();
    }

}
